<template>
  <div class="px16">
    <div
      class="top"
      @click="
        () => {
          this.$router.go(-1);
        }
      "
    >
      X
    </div>
    <img src="../images/elemelogo.webp" alt="" />
    <input
      type="text"
      placeholder="请输入用户名"
      v-model="username"
      @focus="cleartext()"
    />
    <input
      type="password"
      placeholder="请输入密码"
      v-model="password"
      @focus="cleartext()"
    />
    <div class="login" @click="login()">登录</div>
    <div
      class="register"
      @click="
        () => {
          this.$router.push('/register');
        }
      "
    >
      注册
    </div>
    <span>{{ text }}</span>
  </div>
</template>

<script>
import { getlink } from "@/api/getlink.js";
export default {
  data() {
    return {
      username: "",
      password: null,
      text: "",
    };
  },
  methods: {
    cleartext() {
      this.text = "";
    },
    login() {
      if (this.username == "" || this.password == "") {
        this.text = "请输入用户名密码";
      } else {
        getlink("/data/user", "post", {
          username: this.username,
          password: this.password,
        }).then((ok) => {
          if (ok.data == "error") {
            this.text = "用户名密码错误";
          } else {
            console.log(ok.data);
            this.text = "登录成功,即将跳转到首页";
            this.$store.commit("upuserid", { userid: ok.data[1] });
            console.log(this.$store.state.userid);
            this.$store.commit("upuser", { username: this.username });

            setTimeout(() => {
              this.$router.push("/shouye");
            }, 1000);
          }
        });
      }
    },
  },
};
</script>

<style scoped>
.px16 {
  font-size: 0.16rem;
}
.top {
  height: 0.54rem;
  width: 100%;
  padding-left: 0.1rem;
  line-height: 0.54rem;
}
img {
  width: 100%;
}
input {
  width: 3.5rem;
  height: 0.36rem;
  margin: 0.14rem 0.14rem 0;
  padding-left: 0.1rem;
}
.login {
  width: 3.5rem;
  height: 0.36rem;
  margin: 0.14rem 0.14rem 0;
  background-color: rgb(92, 202, 245);
  border-radius: 0.2rem;
  text-align: center;
  line-height: 0.36rem;
  color: aliceblue;
  font-weight: 700;
}
.register {
  width: 3.5rem;
  height: 0.36rem;
  margin: 0.14rem 0.14rem 0;
  border-radius: 0.2rem;
  text-align: center;
  line-height: 0.36rem;
  color: rgb(90, 87, 87);
  font-weight: 700;
  border: 0.02rem solid rgb(90, 87, 87);
}
</style>